<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Admin_Agents extends CI_Controller {

    protected $section = 'agents';
    protected $per_page = 10;
    protected $validation_rules = array(
        'first_name' => array(
            'field' => 'first_name',
            'label' => 'lang:agent:label:first_name',
            'rules' => 'required|xss_clean'
        ),
        'last_name' => array(
            'field' => 'last_name',
            'label' => 'lang:agent:label:last_name',
            'rules' => 'required|xss_clean'
        ),
        'email' => array(
            'field' => 'email',
            'label' => 'lang:agent:label:email',
            'rules' => 'required|valid_email'
        ),
        'username' => array(
            'field' => 'username',
            'label' => 'lang:agent:label:username',
            'rules' => 'trim|required|max_length[20]|xss_clean|alpha_numeric'
        ),
        'password' => array(
            'field' => 'password',
            'label' => 'lang:agent:label:password',
            'rules' => '',
        ),
        'password_confirm' => array(
            'field' => 'password_confirm',
            'label' => 'lang:agent:label:password_confirm',
            'rules' => ''
        ),
        'company' => array(
            'field' => 'company',
            'label' => 'lang:agent:label:company',
            'rules' => 'xss_clean'
        ),
        'phone' => array(
            'field' => 'phone',
            'label' => 'lang:agent:label:phone',
            'rules' => 'xss_clean|min_length[11]'
        ),
    );

    function __construct() {
        parent::__construct();

        $this->load->database();
        $this->load->library('ion_auth');
        $this->load->library('form_validation');
        $this->load->library('session');
        $this->load->helper('pagination');
        $this->load->helper('language');

        $this->load->helper('auth');
        $this->load->model('users_m');
        $this->load->model('admin_estate_model');
        $this->load->model('settings_m');
        $this->lang->load('admin_estate');

        is_login(2);
    }

    public function index() {
        $base_where = array('group_slug' => 'agent');

        $total_rows = $this->users_m->count_by($base_where);
        $pagination = create_pagination('admin/agents/index/', $total_rows, $per_page);

        $this->db->limit($pagination['limit'], $pagination['offset']);
        $agents = $this->users_m->get_many_by($base_where);

        $this->data = array(
            'ptitle' => lang('agent:title:manage_agents'),
            'agents' => $agents,
            'pagination' => $pagination,
        );
        $this->session->set_userdata('page', 20);
        $this->data['success'] = $this->session->flashdata('success');
        $this->data['error'] = $this->session->flashdata('error');

        $this->tmadmin->tmView('agents/index', $this->data);
    }

    public function create() {
        $this->validation_rules['password']['rules'] .= 'required';
        $this->validation_rules['password_confirm']['rules'] .= 'required|match[password]';

        $this->form_validation->set_rules($this->validation_rules);

        if ($this->form_validation->run()) {

            $username = $this->input->post('username');
            $email = $this->input->post('email');
            $password = $this->input->post('password');

            $form_data = array(
                'first_name' => $this->input->post('first_name'),
                'last_name' => $this->input->post('last_name'),
                'company' => $this->input->post('company'),
                'phone' => $this->input->post('phone'),
            );

            if ($id = $this->ion_auth->register($username, $password, $email, $form_data, array('6'))) {
                $this->session->set_flashdata('success', $this->ion_auth->messages());
            } else {
                $this->session->set_flashdata('error', $this->ion_auth->errors());
            }

            $this->input->post('btnAction') == 'save_exit' ? redirect('admin/agents') : redirect('admin/agents/edit/' . $id);
        } else {
            $agent = new stdClass;
            foreach ($this->validation_rules as $key => $field) {
                $agent->$field['field'] = set_value($field['field']);
            }

            $this->data['error'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('error')));
            $this->data['success'] = $this->session->flashdata('success');
        }

        $this->data['ptitle'] = lang('agent:title:create_agent');
        $this->data['agent'] = $agent;
        $this->session->set_userdata('page', 20);
        $this->tmadmin->tmView('agents/form', $this->data);
    }

    public function edit($id = 0) {
        $id or redirect('admin/agents');

        $agent = $this->users_m->get($id);

        $this->form_validation->set_rules($this->validation_rules);

        if ($this->form_validation->run()) {

            $form_data = array(
                'username' => $this->input->post('username'),
                'email' => $this->input->post('email'),
                'first_name' => $this->input->post('first_name'),
                'last_name' => $this->input->post('last_name'),
                'company' => $this->input->post('company'),
                'phone' => $this->input->post('phone'),
            );

            if ($this->ion_auth->update($id, $form_data)) {
                $this->session->set_flashdata('success', $this->ion_auth->messages());
            } else {
                $this->session->set_flashdata('error', $this->ion_auth->errors());
            }

            $this->input->post('btnAction') == 'save_exit' ? redirect('admin/agents') : redirect('admin/agents/edit/' . $id);
        } else {
            foreach ($this->validation_rules as $key => $field) {
                if (isset($_POST[$field['field']])) {
                    $agent->$field['field'] = set_value($field['field']);
                }
            }

            $this->data['error'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('error')));
            $this->data['success'] = $this->session->flashdata('success');
        }

        $this->data['ptitle'] = lang('agent:title:edit_agent');
        $this->data['agent'] = $agent;

        $this->tmadmin->tmView('agents/form', $this->data);
    }

    public function activate($id) {
        if ($this->ion_auth->logged_in() and $this->ion_auth->is_admin(4)) {
            $this->ion_auth->activate($id);
            $this->session->set_flashdata('success', lang('agent:activate_success'));
        } else {
            $this->session->set_flashdata('error', lang('agent:activate_error'));
        }
        redirect('admin/agents');
    }

    public function deactivate($id) {
        if ($this->ion_auth->logged_in() and $this->ion_auth->is_admin(4)) {
            $this->ion_auth->deactivate($id);
            $this->session->set_flashdata('success', lang('agent:deactivate_success'));
        } else {
            $this->session->set_flashdata('error', lang('agent:deactivate_error'));
        }
        redirect('admin/agents');
    }

    public function delete($id) {
        if ($this->ion_auth->logged_in() && $this->ion_auth->is_admin(4)) {
            $this->ion_auth->delete_user($id);
            $this->session->set_flashdata('success', lang('agent:delete_success'));
        } else {
            $this->session->set_flashdata('error', lang('agent:delete_error'));
        }
        redirect('admin/agents');
    }

}

/* End of file admin_agents.php */
/* Location: ./application/controllers/admin_agents.php */